/*
 * @(#)TestsetAction.java
 *
 *
 *
 * ver 1.0 Sep 6, 2007 ggranum
 */
package org.testfire.realm.view.json.action;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.json.JSONException;
import org.testfire.realm.control.exception.UpdateException;
import org.testfire.realm.control.testfire.TestFireDBAO;
import org.testfire.realm.control.user.UserBean;
import org.testfire.realm.model.entities.testfire.TestEntity;
import org.testfire.realm.view.json.translator.TestTranslator;

// eof: TestsetAction.java

public class TestAction implements JsonAction<String>
{

    TestFireDBAO testfireDBAO;

    UserBean userBean;

    TestTranslator testTranslator;

    // Inject the lgsrealm entity manager, as defined in the persistence.xml file.
    @PersistenceContext(unitName = "testfire")
    EntityManager em;

    public String perform( String[] argumentKeys, String... args )
        throws UpdateException
    {
        try
        {
            TestEntity translatedObj = testTranslator.toObject( args[1] );
            if ( args[0].equals( "commit" ) )
            {
                // commit only occurs for objects that exist in the database:
                TestEntity test = em.find( TestEntity.class, translatedObj.getId() );
                testTranslator.copyValues( test, translatedObj );
                this.testfireDBAO.merge( test );
            }
            return "success";
        }
        catch ( JSONException e )
        {
            throw new RuntimeException( e );
        }
    }

    public void setTestfireDBAO( TestFireDBAO testfireDBAO )
    {
        this.testfireDBAO = testfireDBAO;
    }

    public void setUserBean( UserBean userBean )
    {
        this.userBean = userBean;
    }

    public void setTestTranslator( TestTranslator testTranslator )
    {
        this.testTranslator = testTranslator;
    }
}